﻿if (window.top != window.self) {
    top.location.href = location.href;
}
layui.use(['form'], function () {
    var $ = layui.jquery;
    var form = layui.form,
        layer = layui.layer;

    // 登录过期的时候，跳出ifram框架
    if (top.location != self.location) top.location = self.location;

    // 粒子线条背景
    $(document).ready(function () {
        $('.layui-container').particleground({
            dotColor: '#7ec7fd',
            lineColor: '#7ec7fd'
        });
    });
    // 生成验证码
    function AjaxCaptcha() {
        $.ajax({
            url: fmtUrl('/home/AjaxCaptcha'),
            method: 'post',
            success: function (d) {
                $("#captchaPic").attr("src", 'data:text/html;base64,' + d.data.img);
                $("#codeID").val(d.data.id);
            }
        });
    }
    AjaxCaptcha();
    $("#captchaPic").click(function () {
        AjaxCaptcha();
    });

    // 进行登录操作
    form.on('submit(login)', function (data) {
        var e = data;
        console.log(e);
        data = data.field;
        var $frm = $(e.form);


        if (data.username == '') {
            layer.msg('用户名不能为空');
            return false;
        }
        if (data.password == '') {
            layer.msg('密码不能为空');
            return false;
        }
        if (data.captcha == '') {
            layer.msg('验证码不能为空');
            return false;
        }
        var loadIndex = layer.load(3);
        $.ajax({
            url: $frm.attr("action"),
            data: data,
            method: 'post',
            success: function (d) {
                if (d.resultCode == "200") {
                    layer.msg('登录成功', function () {
                        top.location = baseUrl;
                        layer.close(loadIndex);
                    });

                } else {

                    layer.msg(d.message);

                    layer.close(loadIndex);
                }
                // alert(d)
            },
            error: function (e) {
                console.log(e);
                layer.close(loadIndex);
            }
        });

        return false;
    });
});